package com.csesteel.crm.web;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2015/5/28 0028.
 */
public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        String parentPath=request.getContextPath();
        HttpSession session=request.getSession();
        String username = (String)session.getAttribute("username");
//        if(username == null){
//            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
//            return false;


//        }
        if(null==request.getSession(false)){
            if(true==request.getSession(true).isNew()){
            }
            else{
                System.out.println("session已经过期");
                request.getRequestDispatcher("/crm/getLoginPage.do").forward(request,response);
                return false;
            }
        }
        return true;
    }
    /**
     * 在业务处理器处理请求执行完成后,生成视图之前执行的动作
     */
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }
    /**
     * 在DispatcherServlet完全处理完请求后被调用
     *   当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     */
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
